<?php
/*
 * 各种实用方法
 *
 */
class LibsCommon {
    private static $_config = null;

    /*
     * 格式化输入数组
     *
     * @param array $params array 
     *
     * @return void;
     */
    public static function printArr(array $params) {
        echo '<pre>';
        print_r($params);
        echo '</pre>';
    }
        
    /*
     * 重定向
     *
     * @param url 重定向to url  url为'controller/action'  test/index
     * @param $time 过多久才重定向
     *
     * @return void
     */
    public static function redirect($url='',$time = 0){
        $url = self::getDomainUrl().'/'.$url;
        $url = rtrim($url, '/');
        if(!$time){
            header('Location:'.$url);
        }else{
            header("Refresh:$time;url=$url");
        }
    }

    /**
     * 获取/abc/index.php
     *
     * @return string
     */
    public static function getDomainUrl() {
        //apache 不会包含index.php后面controller/action
        //nginx 会包含后面的
        if (($seo = self::getConfig('seo')) && !empty($seo)) {
            return '';
        }
        $arr = explode('/index.php', $_SERVER['SCRIPT_NAME']);
        return $arr[0].'/index.php';
    }
    
    /**
     * 输出一个<a></a>
     * <?php lib_functions::action('test/add', 'ADD', array('id'=>'add_id','class'=>'add_class'));
     * <=>  <a class="add_class" id="add_id" href="/front_controller/index.php/test/add">ADD</a>
     * @param $url <a href=$url>
     * @param $name <a>$name</a>
     * @param attributes 各种属性
     */
    public static function action($url,$name,array $attributes =NULL){
        $u = self::getDomainUrl().'/'.$url;
        $attr = '';
        if(!is_null($attributes)){
            foreach($attributes as $key => $val){
                $attr .= " $key='{$val}'";
            }
        }
        return "<a href='{$u}' $attr>$name</a>";
    }

    public static function url($url) {
        return self::getDomainUrl().'/'.$url;
    }
    
    /*
     * 增加布局文件
     * 
     * @param string $file file
     *
     * @return void
     */
    public static function addLayout($file){
        if(defined('ROOT_PATH')) {
            include ROOT_PATH.DIRECTORY_SEPARATOR.'layouts'.DIRECTORY_SEPARATOR.$file;
        }
    }
    /*
     * 获取data目录array文件
     * 
     * @param string $file file
     *
     * @return array
     */
    public static function getDataArray($file){
        if(defined('ROOT_PATH')) {
            return include ROOT_PATH.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.$file;
        }
    }

    /**
     * 返回include文件内容成为一个变量
     *
     * @param string $filename 文件名
     *
     * @return string|null
     */
    public static function fetchTpl($filename) {
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }

    /**
     * 获取域名的根目录不包含index.php 
     *
     * @return string
     */
    public static function getRootUrl() {
        $arr = explode('index.php', $_SERVER['PHP_SELF']);
        return $arr[0];
    }

    /**
     * 输出图片
     *
     * @return string
     */
    public static function image($url, array $attributes = array(), $isReal=false) {
        $url = $isReal ? $url : (self::getRootUrl().'layouts/images/'.$url);
        $str = "<img src='{$url}'";

        if ($isReal && empty($attributes['onerror'])) {
            $urlTmp = self::url('image/index/').base64_encode($url);
            if (($seo = self::getConfig('seo')) && !empty($seo)) {
                $urlTmp .= '/'.date('Ym').'jpg';
            }
            $attributes['onerror'] = "this.src='{$urlTmp}';";
        }
        foreach ($attributes as $k => $v) {
            $str .= " {$k}=\"{$v}\"";
        }
        $str .= ' />';
        return $str;

    }

    /**
     * 引入第三方库
     *
     *
     * @return boolean 
     */
    public static function importThirdLibrary($dirFile, $file=null)
    {
        $path = ROOT_PATH.'/libs/thirds/'.$dirFile;
        if (!is_dir($path)) {
            return false;
        }
        if (!empty($file)) {
            require_once $path.'/'.$file;
            return true;
        }

        $list = scandir($path);
        foreach ($list as $v) {
            if (!preg_match('#\w+(\.\w+)*\.php#', $v)) {
                continue; 
            }
            require_once $path.'/'.$v;
        }
        return true;
    }

    /**
     * 获取config.php配置信息
     *
     * @return array
     */
    public static function getConfig($key = null)
    {
        is_null(self::$_config) && (self::$_config = include ROOT_PATH. '/data/config.php');
        if (empty($key)) {
            return self::$_config;
        } 
        return isset(self::$_config[$key]) ? self::$_config[$key] : null;
    }

}
